/*
 * Copyright (C) 2010, JDevelopers (jdevelopers@my-pages.org) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.jdevelopers.xarf;

import java.io.Serializable;
import java.util.List;
import java.util.Vector;

import javax.mail.Address;

/**
 * Object, which will contain all needed information of the report mail. The human readable part 
 * is usually part one in the report. The part, which could be processed by applications is part 
 * two in the report. This part will be splitted into the separate lines and added to the 
 * reportProperty list in this object. The last part of a report mail could be an attachment. If 
 * the content type of the attachment is "text/plain", the attachment will be set in "textAttachment".
 * If the content type of the attachment is "message/rfc822" all mail headers will be added 
 * to "attachmentsMailHeaders".
 * 
 * @author Marcel Hartwig
 * @version 0.2
 * @since JXarfValidator 0.1
 */
public abstract class XarfReport implements Serializable {

	/**
     * ID for serializing the report.
     */
    private static final long serialVersionUID = 2074286484445123596L;
    private String reportSubject;
	private Address reportSender;
	private String humanReadablePart;
	private List<XarfReportProperty> reportProperties = null;
	private EXarfReportAttachmentType attachmentType = EXarfReportAttachmentType.NOT_SPECIFIED;
	private String textAttachment = null;
	private List<XarfReportAttachmentMailHeader> attachmentsMailHeaders = null;
	private EReportType reportType;
	
	/**
	 * Constructor for creating an instance of a XarfReport. 
	 */
	public XarfReport() {
	}
	
	/**
	 * Constructor for creating an instance of a XarfReport specified by the report type.
	 * 
	 *  @param reportType the type of the report.
	 */
	public XarfReport(EReportType reportType) {
		setReportType(reportType);
	}

	/**
	 * Returns the human readable part of the report.
	 * 
	 * @return the human readable part as a String.
	 */
	public String getHumanReadablePart() {
		return humanReadablePart;
	}

	/**
	 * Sets the human readable part of the report.
	 * 
	 * @param humanReadablePart the humanReadablePart to set
	 */
	protected void setHumanReadablePart(String humanReadablePart) {
		this.humanReadablePart = humanReadablePart;
	}
	
	/**
	 * Returns the subject of the parsed report.
	 * 
	 * @return the subject of the report.
	 */
	public String getReportSubject() {
		return reportSubject;
	}

	/**
	 * Sets the subject of the parsed report.
	 * 
	 * @param reportSubject the subject of the report.
	 */
	protected void setReportSubject(String reportSubject) {
		this.reportSubject = reportSubject;
	}
	
	/**
	 * Returns the email address of the report.
	 * 
	 * @return the report sender address.
	 */
	public Address getReportSender() {
		return reportSender;
	}

	/**
	 * Sets the sender address of the report.
	 * 
	 * @param reportSender the sender address of the report.
	 */
	protected void setReportSender(Address reportSender) {
		this.reportSender = reportSender;
	}

	/**
	 * Method for adding a XarfReportProperty to the report.
	 * 
	 * @param reportProperty the XarfReportProperty to be added.
	 */
	protected void addReportProperty(XarfReportProperty reportProperty) {
		if(reportProperties == null) {
			reportProperties = new Vector<XarfReportProperty>();
		}
		reportProperties.add(reportProperty);
	}
	
	/**
	 * Method for checking, if the report has XarfReportProperties.
	 * 
	 * @return <code>true</code> if the report has XarfReportProperties, otherwise <code>false</code>
	 */
	public boolean hasReportProperties() {
		return reportProperties != null && !reportProperties.isEmpty();
	}
	
	/**
	 * Returns all XarfReportProperties of the report.
	 * 
	 * @return XarfReportProperties, attached to the report.
	 */
	public XarfReportProperty[] getReportProperties() {
		if(reportProperties != null) {
			return reportProperties.toArray(new XarfReportProperty[reportProperties.size()]);
		}
		return new XarfReportProperty[0];
	}
	
	/**
	 * Returns a specific XarfReportProperty.
	 * 
	 * @param propertyName the name of the XarfReportProperty, that should be returned.
	 * @return the stored XarfReportProperty. If the property is not available, <code>NULL</code> will be returned.
	 */
	public XarfReportProperty getXarfReportProperty(String propertyName) {
		if(reportProperties != null) {
			for(XarfReportProperty property : reportProperties) {
				if(property.getPropertyName().equalsIgnoreCase(propertyName)) {
					return property;
				}
			}
		}
		return null;
	}

	/**
	 * Returns the type of the attachment of the report.
	 * 
	 * @return the type of the attachment.
	 */
	public EXarfReportAttachmentType getAttachmentType() {
		return attachmentType;
	}

	/**
	 * Sets the type of the attachment.
	 * 
	 * @param attachmentType the attachmentType to set.
	 */
	protected void setAttachmentType(EXarfReportAttachmentType attachmentType) {
		this.attachmentType = attachmentType;
	}

	/**
	 * Returns the text attachment of the report.
	 * 
	 * @return the textAttachment.
	 */
	public String getTextAttachment() {
		return textAttachment;
	}

	/**
	 * Sets the text attachment of the parsed report.
	 * 
	 * @param textAttachment the textAttachment to set.
	 */
	protected void setTextAttachment(String textAttachment) {
		this.textAttachment = textAttachment;
	}
	
	/**
	 * Adds a XarfReportAttachmentMailHeader to the report.
	 *  
	 * @param header the header to be added.
	 */
	protected void addXarfReportAttachmentMailHeader(XarfReportAttachmentMailHeader header) {
		if(attachmentsMailHeaders == null) {
			attachmentsMailHeaders = new Vector<XarfReportAttachmentMailHeader>();
		}
		attachmentsMailHeaders.add(header);
	}
	
	/**
	 * Adds a list of XarfReportAttachmentMailHeader to the report.
	 * 
	 * @param headers the headers to be added.
	 */
	protected void addAllXarfReportAttachmentMailHeaders(List<XarfReportAttachmentMailHeader> headers) {
		if(attachmentsMailHeaders == null) {
			attachmentsMailHeaders = new Vector<XarfReportAttachmentMailHeader>();
		}
		attachmentsMailHeaders.addAll(headers);
	}
	
	/**
	 * Returns a XarfReportAttachmentMailHeader, specified by the name of the header. If the 
	 * header is not available, <code>NULL</code> will be returned.
	 * 
	 * @param headerName the name of the header, which should be returned.
	 * @return the XarfReportAttachmentMailHeader.
	 */
	public XarfReportAttachmentMailHeader getXarfReportAttachmentMailHeader(String headerName) {
		if(attachmentsMailHeaders != null) {
			for(XarfReportAttachmentMailHeader header : attachmentsMailHeaders) {
			    //Fix: because some ISP's do not let the headers as thea are, we need to get
			    //the header name by ignoring the casesensivity
				if(header.getHeaderName().equalsIgnoreCase(headerName)) {
					return header;
				}
			}
		}
		return null;
	}
	
	/**
	 * Returns all XarfReportAttachmentMailHeaders of the report.
	 * 
	 * @return all XarfReportAttachmentMailHeaders
	 */
	public XarfReportAttachmentMailHeader[] getXarfReportAttachmentMailHeaders() {
		if(attachmentsMailHeaders != null) {
			return attachmentsMailHeaders.toArray(new XarfReportAttachmentMailHeader[attachmentsMailHeaders.size()]);
		}
		return null;
	}
	
	/**
	 * Checks, if the report has any XarfReportAttachmentMailHeaders.
	 *  
	 * @return <code>true</code> if the report has any XarfReportAttachmentMailHeader, otherwise <code>false</code>
	 */
	public boolean hasXarfReportAttachmentMailHeaders() {
		return attachmentsMailHeaders != null && !attachmentsMailHeaders.isEmpty();
	}

	/**
	 * Returns the type  of the report.
	 * 
	 * @return the reportType
	 */
	public EReportType getReportType() {
		return reportType;
	}

	/**
	 * Sets the type of the report.
	 * 
	 * @param reportType the reportType to set
	 */
	public void setReportType(EReportType reportType) {
		this.reportType = reportType;
	}
	
	
	
}
